package com.bigShen.pattern.proxy.dynamic;

import com.bigShen.pattern.proxy.Person;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author: lei.shen
 * @date: 2018/10/2
 * @desc: JDK动态代理, 只能代理接口, 所以被代理类必须实现接口
 */
public class LoveAgentJDK implements InvocationHandler {

    private Object element;

    public LoveAgentJDK(Object element) {
        this.element = element;
    }

    public Object getInstance() {

        Object obj =  Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{Person.class}, this);
        return obj;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        helpFindLove();
        method.invoke(element, args);
        afterFindLove();
        return null;
    }

    public void helpFindLove() {

        System.out.println("婚介所, 为 " + " 寻找并介绍对象!");

    }

    public void afterFindLove() {

        System.out.println("婚介所，为 "  +  " 介绍成功后,收费!");

    }

}
